<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>仓库供应商库存查询列表页面</title>

<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.3.2.min.js"	type="text/javascript"></script>
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>

<style type="text/css">
#searchbar{
   margin-bottom: 10px;
   margin-left:15px;
  }
  .navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
.l-button-search {
	width: 50px;
	margin-left: 10px;
	padding-bottom: 2px;
	display: inline;
}
</style>
</head>

<body style="padding:6px; overflow:hidden;">
<div class="navbar">
		<span>您当前的位置：</span>
		商品管理 &gt; 次品单管理  &gt;选择商品
</div>

<form id="form1" name="form1" action="getCommodityList.do" method="post" >
<%-- <div id="searchbar">
货品编号：<input type="text" id="prodNo" name="prodNo" value="${productStockVo.prodNo}">
商品名称：<input type="text" id="commoName" name="commoName" value="${productStockVo.commoName}" />&nbsp;
         <input type="submit" id="btnSearch" value="查询" class="l-button-search" />
           <input type="button" id="btnReset" value="重置" class="l-button-search" />
</div> --%>
</form>
<div id="maingrid" style="margin:0; padding:0"></div>
<script>
	var isSelectOnlyRow =  ${selectOnlyRow};
	var basePath='${BasePath}';
	var grid = null;
	/* $(function() {
		$("#btnSearch").click(function() {
			var sel = $("#prodNo").val()+$("#commoName").val();
			if(window.parent.sel != sel){
				window.parent.list = new Array();
			}
			window.parent.sel = sel;
			$("#form1").submit();
		});
	}); */

    
    $(function () {
    	 // 重置
        $("#btnReset").click(function(){
    		$('#searchbar :input').not(':submit, :button, :reset').val('').removeAttr('checked').removeAttr('selected');
    	});
    	
        grid = $("#maingrid").ligerGrid({
        	checkbox: !isSelectOnlyRow,
            columns: [
            { display: '商品编号', name: 'commoNo'},
            { display: '商品名称', name: 'commoName'},
        	{ display: '货品编号', name: 'prodNo'},
         	{ display: '采购单号', name: 'poNo'},
            { display: '规格', name: 'specName'},
            { display: '仓库', name: 'whName'},
            { display: '供应商', name: 'supName'},
            { display: '库存良品数', name: 'yieldAmountNumner'},
            ], 
            pageSize:30,
            width: '100%',
            height: '100%',
            dataType: 'server', 
            dataAction: 'server',
            pagesizeParmName: 'pageSize',
            parms: [
                    /* {name: "prodNo", value:$("#prodNo").val()},
                    {name: "commoName", value:$("#commoName").val()} */
            	],
            url: basePath + "/manager/StockPurchaser/ajaxPageFinderStockPurchaser.do",
            selectRowButtonOnly: true,
            isChecked: F_ISCHECKED,
            onSelectrow :F_SELECTROW,
            onBeforeCheckRow: F_BEFORECHECKROW,
			onBeforeCheckAllRow: F_BEFORECHECKALLROW
        });
    });
    
    
    function getDefective(data){ //转换成defective
    	var product = {
    			prodNo:data.prodNo,
    			commoNo:data.commoNo,
				commoName:data.commoName,
				poNo:data.poNo,
				supCode:data.supNo,
				supName:data.supName,
				whName:data.whName,
				whNo:data.whNo,
				specName:data.specName,
				yieldAmountNumner : data.yieldAmountNumner,
				storageMethods:3
		};
    	return product;
    }
    
    function F_ISCHECKED(rowdata) {//初始化事件
    	if(isSelectOnlyRow){
    	var product = window.parent.product;
    		if(product.prodNo == rowdata.prodNo){
				return true;
			}
    	}else{
    		var list = window.parent.list;
    	   	if(list.length > 0){
    	   		for ( var i = 0; i < list.length; i++) {
    	   			if(list[i].prodNo == rowdata.prodNo){
    	   				return true;
    				}
    			}
    	   	}
    	}
    	return false;
	}
    
    function F_SELECTROW(rowdata, rowid, rowob){
    	if(isSelectOnlyRow){
    		window.parent.product = getDefective(rowdata);
    	}
    }
    function F_BEFORECHECKROW(checked, data, rowindex, rowob){//选择前事件
    	if(!isSelectOnlyRow){
        	updateData(checked, data);
        	if(!isChecked) return isChecked;
    	}
	}
    
    function F_BEFORECHECKALLROW(checked, gridelement){//多选事件
		var data = new Array();
		data = grid.getData();
		var list = new Array();
		list = window.parent.list;
		for ( var i = 0; i < data.length; i++) {
			var isto = true;
    		for ( var j = 0; j < list.length; j++) {
    			if(list[j].prodNo == data[i].prodNo){
    				isto = false;
    				continue;
    			};
			}
    		if(isto || !checked) updateData(checked, data[i]);
		}
    }

    var isChecked = true;
    function updateData(checked, data){
    	if(isSelectOnlyRow){
    		return 
    	}
    	isChecked = true;
    	if(checked){
    		var list = new Array();
    		list = window.parent.commoditylist;
    		var tx = "";
    		for ( var i = 0; i < list.length; i++) {
				if(list[i].commoNo == data.commoNo){
					tx = data.commoName+"商品已添加！";
				};
			};
			if(tx != ""){
				$.ligerDialog.warn(data.commoName+"商品已添加！");
				isChecked = false;
				return false;	
			}
			var product = getDefective(data)
    		window.parent.list.push(product);
    	}else{
    		var list = new Array();
    		list = window.parent.list;
    		for ( var i = 0; i < list.length; i++) {
    			if(list[i].prodNo == data.prodNo){
    				window.parent.list.splice(i,1);
    			};
			}
    	};
    };
    
    var isOut = true;
    function valProd(){
    	if(isSelectOnlyRow){
    		return 
    	}
    	isChecked = true;
    	if(window.parent.list.length == 0){
    		var atl = grid.getSelectedRows().length > 0 ? "这些商品已选择，请重新选择商品" : "请选择商品";
			$.ligerDialog.warn(atl);
			isOut = false;
		}
    }
</script>
</body>
</html>